// pages/sign/sign.js
import WxValidate from '../../utils/WxValidate.js'

const app = getApp()
const phoneReg = /^1\d{10}$/;
Page({
  data: {
    phone: '',
    code: '',
    sms:'',
    password:'',
    wx_user_info:{
      nickName:"",
      avatarUrl:''
    },
    checked: false,
    count: 60,//验证码
    agree: [],
  },
  onLoad(options) {
    let that = this;
    that.setData({
      wx_user_info:{
        nickName: options.nickName,
        avatarUrl: options.avatarUrl
      },
      code:options.code
    })
  },
  checkboxChange(e) {
    let that = this;
    that.setData({ agree: "" })

    var val = e.detail.value;
    if (val) {
      that.setData({ agree: val })
    } else {
      that.setData({ agree: [] })
    }

  },
  xieyi(){
    wx.navigateTo({
      url: '../xieyi/xieyi',
    })
  },
  // 获取手机号
  getPhone(e){
    this.setData({ phone: e.detail.value })
  },
  // 获取手机验证码
  getVcode: function () {
    let that = this;
    // console.log(1)
    let phone = that.data.phone;
    console.log(phone)
    
    if (!phoneReg.test(phone)) {
      wx.showModal({
        title: '提示',
        content: '请填入正确的手机号',
        showCancel: false
      })
      return;
    }
    var date = new Date();
    var m = date.getMinutes().toString();//获得当前分钟数
    // console.log('提交外面的')
    // console.log(m)
    // console.log(date.getMinutes())
    //开始缓存池中没有分钟数，当前分钟数肯定不等于缓存中的分钟数，当前可以执行
    if (m != wx.getStorageSync('m')) {
      wx.setStorageSync('m', m)//把分钟数放到缓存
      app.post('/login/send_sms', "post", { mobile: phone }).then((res) => {
        let timer = setInterval(() => {
          let count = that.data.count;
          count--;
          that.setData({ count: count });
          if (count == 0) {
            clearInterval(timer);
            that.setData({ count: 60 });
          }
        }, 1000)
      })
    }else{
      //当发生了1分钟内多次点击等事件，弹窗提示或者做别的操作。
      wx.showToast({
        title: '请不要重复点击',
        duration: 1000
      })
    }
    
  },
  showModal(error) {
    wx.showModal({
      content: error.msg,
      showCancel: false,
    })
  },
  // 点击注册
  formSubmit(e){
    let that = this;
    let form = e.detail.value;
    if (!phoneReg.test(form.username)) {
      wx.showModal({
        title: '提示',
        content: '请输入正确的手机号',
        showCancel: false
      })
      return;
    }
    if (!form.sms) {
      wx.showModal({
        title: '提示',
        content: '请输入验证码',
        showCancel: false
      })
      return;
    }
    
    if (!form.password) {
      wx.showModal({
        title: '提示',
        content: '请输入密码',
        showCancel: false
      })
      return;
    }
    if (form.password<6) {
      wx.showModal({
        title: '提示',
        content: '请输入密码大于6位',
        showCancel: false
      })
      return;
    }
    if (form.checkbox.length == 0) {
      wx.showModal({
        title: '提示',
        content: '请勾选用户协议',
        showCancel: false
      })
      return;
    }
    wx.login({
      success: function (res) {
        let postData = {
          code: res.code,
          wx_user_info: that.data.wx_user_info,
          ...form
        }
        app.post("/login/register", "post", postData).then(res => {
          that.showModal("登录成功")
          setTimeout(function(){
            wx.switchTab({
              url: '../index/index'
            })
          },500)
         
          wx.setStorageSync("tel", that.data.phone);
          wx.setStorageSync("token", res.token);
        })
      }
    })
    
  },
})